#include <stdio.h>
#include <stdlib.h>

int array[100][100];

void squareMatrix(int left, int right, int up, int down, int v, int n)
{
  int x, y, i, j, k, m;
  if (left <= right)
  {
    for (i = left; i <= right; i ++) array[up][i] = v++;
    for (k = up + 1; k < down; k ++) array[k][right] = v++;
    for (j = right; j > left; j --) array[down][j] = v++;
    for (m = down; m > up; m --) array[m][left] = v++;
    squareMatrix(left + 1, right - 1, up + 1, down - 1, v, n);
  }
  else
  {
    for (x = 0; x < n; x ++)
    {
      for (y = 0; y < n; y ++)
      {
        printf("%4d", array[x][y]);
      }
      printf("\n");
    }
  }
}

int main()
{
  squareMatrix(0, 4, 0, 4, 1, 5);
  system("pause");
  return 0;
}